<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['complainservice'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['complainservice'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  /////////////////////date/////////////////
   $msg ="";
  if(isset($_REQUEST['toDateYear']))
  {
	  $toDate    = $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-".$_REQUEST['toDateDay']." 24:59:59";
  }
  else
  {
	 // $toDate    = date("Y-m-d");
	}
	if(isset($_REQUEST['fromDateYear']))
  {
	  $fromDate    = $_REQUEST['fromDateYear']."-".$_REQUEST['fromDateMonth']."-".$_REQUEST['fromDateDay']." 00:00:00";
  }
  else
  {
	  $fromDate    = date("Y-m-d");
	}
	/////////////////////date/////////////////
  $dailyReportDetailArray  = array();
  $dailyReportCount        = 0;
  $rownum        = 0;
  $selectdailyReportEntry = "SELECT dailyreportmaster.dailyReportMasterId,user.userId,user.userName,DATE_FORMAT(dailyreportmaster.reportDate,'%d-%m-%Y') AS reportDate,
                                    DATE_FORMAT(dailyreportmaster.createDate,'%d-%m-%Y') AS createDate,dailyreportdetail.dailyReportMasterId,
                                    party.partyId,party.companyName,dailyreportdetail.outCome,dailyreportdetail.remarks,
                                    DATE_FORMAT(dailyreportdetail.nextFollowUp,'%d-%m-%Y') AS nextFollowUp,typeofconclusion.typeOfConclusionId,typeofconclusion.typeOfConclusion
				  						         FROM dailyreportmaster
				  						         JOIN dailyreportdetail
					 						         JOIN user   
						 					         JOIN party
											         JOIN typeofconclusion 
											        WHERE user.userId      = dailyreportmaster.userId
											          AND dailyreportdetail.dailyReportMasterId = dailyreportmaster.dailyReportMasterId 
											          AND party.partyId    = dailyreportdetail.partyId 
											          AND typeofconclusion.typeOfConclusionId = dailyreportdetail.typeOfConclusionId";            
  if(isset($_GET['userId']))
  {											          
    if($_GET['userId'] > 0 )
    {
      $selectdailyReportEntry .= " AND dailyreportmaster.userId = ".$_GET['userId'];                  
    }
  } 
  if(isset($_GET['toDateMonth'] ) && $_GET['toDateMonth'] != '')
  {
    $selectdailyReportEntry .= " AND dailyreportmaster.reportDate >= '".$fromDate."'
                                 AND dailyreportmaster.reportDate <= '".$toDate."'";
  }                               
  $selectdailyReportEntry .= " ORDER BY dailyreportmaster.dailyReportMasterId,dailyreportmaster.reportDate ";
  								          
    $selectdailyReportEntryResult = mysql_query($selectdailyReportEntry);
    while($dailyReportRow = mysql_fetch_array($selectdailyReportEntryResult))
	  {
	  	if(isset($dailyReportDetailArray[$dailyReportCount - 1]['dailyReportMasterId']) 
       && $dailyReportDetailArray[$dailyReportCount - 1]['dailyReportMasterId']  == $dailyReportRow['dailyReportMasterId'])// && date prev and current both same
      {
        $dailyReportDetailArray[$dailyReportCount]['display'] = 0;
      }	 
      else
      {
    	  $dailyReportDetailArray[$dailyReportCount]['display'] = 1;
    	  $rownum++;
      } 
      if($dailyReportRow['nextFollowUp'] == '00-00-0000')
      {
        $dailyReportDetailArray[$dailyReportCount]['nextFollowUp']         = "";
      }	
      else
      {
        $dailyReportDetailArray[$dailyReportCount]['nextFollowUp']         = $dailyReportRow['nextFollowUp'];
      }
		  $dailyReportDetailArray[$dailyReportCount]['rownum']  = $rownum;
		  $dailyReportDetailArray[$dailyReportCount]['dailyReportMasterId']  = $dailyReportRow['dailyReportMasterId'];
	    $dailyReportDetailArray[$dailyReportCount]['userName']             = $dailyReportRow['userName'];
		  $dailyReportDetailArray[$dailyReportCount]['reportDate']           = $dailyReportRow['reportDate'];
		  $dailyReportDetailArray[$dailyReportCount]['createDate']           = $dailyReportRow['createDate'];
		  $dailyReportDetailArray[$dailyReportCount]['companyName']          = $dailyReportRow['companyName'];
		  $dailyReportDetailArray[$dailyReportCount]['outCome']              = $dailyReportRow['outCome'];
		  $dailyReportDetailArray[$dailyReportCount]['remarks']              = $dailyReportRow['remarks'];
		  $dailyReportDetailArray[$dailyReportCount]['typeOfConclusion']     = $dailyReportRow['typeOfConclusion'];
		  $dailyReportCount++;
	  }  
  if($dailyReportCount == 0) 
  {
	  $msg = 'Record Not Found...!';
  }
  include("./bottom.php");
  $smarty->assign("msg",$msg);
  $smarty->assign("rownum",$rownum);
  $smarty->assign("dailyReportDetailArray",$dailyReportDetailArray);
  $smarty->display("dailyReportListAj.tpl");
}
?>